package com.bw.wxy.mvp.model;

import com.bw.wxy.common.Api;
import com.bw.wxy.mvp.model.entity.BaseEntity;
import com.bw.wxy.mvp.model.entity.ResponseUserEntity;
import com.bw.wxy.factroy.ChangFunction;
import com.bw.wxy.factroy.HttpCode;
import com.bw.wxy.factroy.HttpFactory;
import com.bw.wxy.factroy.HttpType;
import com.bw.wxy.mvp.base.model.BaseModel;
import com.bw.wxy.mvp.contract.LoginContract;
import com.bw.wxy.mvp.model.entity.UploadEntity;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class LoginModel extends BaseModel implements LoginContract.ILoginModel {
    @Inject
    public LoginModel() {
    }

    @Override
    public Observable<BaseEntity> request(Map<String, Object> map, String file) {
        int code = (int) map.get("code");
        map.remove("code");
        if (code == HttpCode.LOGINCODE) {
            RequestBody body = createBody(map);
            Observable<ResponseUserEntity> ob = HttpFactory.getInstance().factory(HttpType.TOKENTYPE)
                    .getRetrofit().create(Api.class).requestLogin(body);
            return ob.map(new ChangFunction<ResponseUserEntity>());
        } else if (code == HttpCode.REGISTERCODE) {
            RequestBody body = createBody(map);
            Observable<ResponseUserEntity> ob = HttpFactory.getInstance().factory(HttpType.TOKENTYPE)
                    .getRetrofit().create(Api.class).requestRegister(body);
            return ob.map(new ChangFunction<ResponseUserEntity>());
        } else if (code == HttpCode.UPDATEUSERCODE) {
            RequestBody body = createBody(map);
            Observable<ResponseUserEntity> ob = HttpFactory.getInstance().factory(HttpType.TOKENTYPE)
                    .getRetrofit().create(Api.class).requestUpdateUser(body);
            return ob.map(new ChangFunction<ResponseUserEntity>());
        } else if (code == HttpCode.UPLOADIMGCODE) {
            RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), new File(file));
            MultipartBody.Builder builder = new MultipartBody.Builder();
            builder.addFormDataPart("file", System.currentTimeMillis() + ".jpg", body);
//            builder.addFormDataPart("userid",user_id+"");
            List<MultipartBody.Part> list = builder.build().parts();
            Observable<UploadEntity> ob = HttpFactory.getInstance().factory(HttpType.UPLOADTYPE)
                    .getRetrofit().create(Api.class).requestUploadHead(list);
            return ob.map(new ChangFunction<UploadEntity>());
        }
        return null;
    }

//    @Override
//    public Observable<BaseEntity> loginRequest(Map<String, Object> map) {
//        RequestBody body=createBody(map);
//        Observable<LoginEntity>ob= HttpFactory.getInstance().factory(HttpType.TOKENTYPE)
//                .getRetrofit().create(Api.class)
//                .requestLogin(body);
//        return ob.map(new ChangFunction<LoginEntity>());
//    }
}

